package com.slz3.business.module.templateExample.temStyle1.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

public class TemStyle1Factory {

    /**
     * 初始化基础属性值
     */
    public static TemStyle1 initBaseFields(TemStyle1 temStyle1) {
        if (temStyle1.getUuid() == null) {
            temStyle1.setUuid(UUIDUtils.generateUUID());
            temStyle1.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(temStyle1.getSerAudit())) {
                temStyle1.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(temStyle1.getSerUsing())) {
                temStyle1.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            temStyle1.setLastModifyTime(LocalDateTime.now());
        }
        return temStyle1;
    }

    /**
     * 初始化基础属性值
     */
    public static TemStyle1 initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static TemStyle1 newTemStyle() {
        return new TemStyle1();
    }

    public static TemStyle1Condition newTemStyle1Condition(MultiValueMap<String, String> params) {

        TemStyle1Condition condition = new TemStyle1Condition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        List<String> serAuditList = params.get("serAuditList");

        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, TemStyle1Condition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, TemStyle1Condition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, TemStyle1Condition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, TemStyle1Condition.CREATE_TIME_OF_PATTERN));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : serAuditList.stream().map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));

        return condition;

    }

}
